window.onload = function () {
    function getCookie(key) {
        let cookies = document.cookie
        if (!cookies) {
            return
        }
        let cookiesArr = cookies.split('; ')
        for (let a = 0; a < cookiesArr.length; a++) {
            let brr = cookiesArr[a].split('=')
            if (brr[0] === key) {
                return brr[1]
            }
        }
    }
    function request(obj) {
        // 限制各个数据
        // 请求路径
        if (obj.url === undefined) {
            throw new Error('请求路径不能为空！')
        }
        // 判断类型
        if (typeof obj.url != 'string') {
            throw new Error('请求路径错误！')
        }
        // dataType
        if (obj.dataType === undefined) {
            obj.dataType = 'json'
        }
        // 请求方式 - get/put/delete/post都可以，大小写都可以，可以不传，默认get
        if (obj.method === undefined) { // 如果没有传递
            // 给默认值get
            obj.method = 'get'
        }
        // 说明get传递了 - 限制是否是这4种方式
        if (obj.method.toLowerCase() != 'get' && obj.method.toLowerCase() != 'post' && obj.method.toLowerCase() != 'put' && obj.method.toLowerCase() != 'delete') {
            // 抛出自定义错误
            throw new Error('请求方式不正确！')
        }
        // async
        if (obj.async === undefined) {
            obj.async = true
        }
        if (typeof obj.async != 'boolean') {
            throw new Error('同步false异步true！')
        }
        // success
        if (obj.success === undefined) {
            obj.success = () => { }
        }
        if (typeof obj.success != 'function') {
            throw new Error('success是函数！')
        }
        // error
        if (obj.error === undefined) {
            obj.error = () => { }
        }
        if (typeof obj.error != 'function') {
            throw new Error('error是函数！')
        }
        // data - 可以是查询字符串，方便传递的话个，给对象更好
        // ajax语法中要求必须是查询字符串 - 如果是对象，需要转成查询字符串
        let data
        if (obj.data != undefined) {
            if (Object.prototype.toString.call(obj.data) === '[object Object]') {
                // 对象 - 转字符串
                let arr = []
                for (let key in obj.data) {
                    arr.push(key + '=' + obj.data[key])
                }
                data = arr.join('&')
            } else if (Object.prototype.toString.call(obj.data) === '[object String]') {
                // 字符串
                data = obj.data
            } else {
                throw new Error('data是字符串或对象！')
            }
            // 判断请求方式是否是get
            if (obj.method.toLowerCase() === 'get') {
                obj.url += '?' + data
            }
        }
        var xhr = new XMLHttpRequest;
        xhr.open(obj.method, obj.url, obj.async);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    let res
                    switch (obj.dataType) {
                        case 'xml':
                            res = xhr.responseXML
                            break
                        case 'json':
                            res = xhr.responseText
                            res = JSON.parse(res)
                            break
                        case 'text':
                            res = xhr.responseText
                            break
                        default:
                            throw new Error('dataType必须是json/text/xml！')
                    }
                    obj.success(res)
                } else {
                    obj.error('错误')
                }
            }
        }
        if (obj.method.toLowerCase() === 'post' && data) {
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
            xhr.send(data)
            return
        }
        xhr.send()
    }
    let name = document.querySelector('.namea')
    console.log(name);
    name.innerHTML = getCookie('username')
    function aa(){
        if (name.innerHTML === 'undefined') {
            popup.msg('还未登录请先登录', () => {
                location.href = '/H5/login.html'
            })
        }else{
            console.log(123);
            console.log(name.innerHTML);
        }
    }
    aa()
    var form = document.querySelector('form')
    var textarea = document.querySelector('textarea')

    //点击添加用户信息
    let tianjia = document.querySelector('.tianjia')
    var zhezhao = document.querySelector('.zhezhao')
    var nei = document.querySelector('.nei')
    let but = document.querySelector('.but')
    console.log(tianjia);
    tianjia.addEventListener('click', function (e) {
        zhezhao.style.display = 'block'
        nei.style.display = 'block'
    })
    //点击按钮关闭
    but.addEventListener('click', function () {
        zhezhao.style.display = 'none'
        nei.style.display = 'none'
    })

    //算鼠标在盒子的坐标、
    nei.addEventListener('mousedown', function (e) {//添加按下事件
        var x = e.pageX - this.offsetLeft//计算鼠标在盒子的x坐标
        var y = e.pageY - this.offsetTop//计算鼠标在盒子的y坐标
        var aa = function (e) {//绑定函数
            var a = e.pageX - x//计算盒子距离左边的位置
            var b = e.pageY - y//计算盒子距离头部的位置
            nei.style.left = a + 'px'//将他赋值给他的left，上面已经添加定位
            nei.style.top = b + 'px'//将他赋值给他的top
        }
        document.addEventListener('mousemove', aa)//绑定添加鼠标移动事件，因为鼠标可能会在页面的任意位置，所以给document添加事件
        document.addEventListener('mouseup', function () {//绑定添加鼠标松开事件，因为鼠标可能会在页面的任意位置松开，所以给document添加事件
            document.removeEventListener('mousemove', aa)//移除绑定事件
        })
    })

    //添加用户
    let jia = document.querySelector('.input')
    let tname = document.querySelector('[name="name"]')
    let pwd = document.querySelector('[name="pwd"]')
    let email = document.querySelector('[name="email"]')
    let radio = document.querySelector('[name="sex"]')
    let iphone = document.querySelector('[name="iphone"]')
    let article = document.querySelector('article table tbody')
    let shu = 0
    jia.addEventListener('submit', function () {
        shu = shu +1
        request({
            method: 'post',
            url: '/front/api/user/conteroller',
            data: {
                username: tname.value,
                password: pwd.value,
                email: email.value,
                sex: radio.value,
                iphone: iphone.value
            },
            success(res) {
                let a = 0
                let { error_code, msg } = res
                console.log(res);
                if (error_code === 200) {
                    //提示登录成功

                    let tbody = ''
                    res.data.aaa.forEach(item => {
                        a++
                        tbody += `
                        <tr>
                            <td>${a}</td>
                            <td>${item.username}</td>
                            <td>${item.iphone}</td>
                            <td>${item.email}</td>
                            <td>
                                <p class="nan">${item.sex}</p>
                            </td>
                            <td>
                                <p class="dis">禁用</p>
                            </td>
                            <td>
                                <button>编辑</button>
                                <button class="del">删除</button>
                            </td>
                        </tr>
                        `
                    })

                    article.innerHTML = tbody
                    popup.msg(msg, () => {
                        zhezhao.style.display = 'none'
                        nei.style.display = 'none'
                    })
                } else {
                    popup.msg(msg)
                }
            }
        })

        return false
    })


    //删除用户
    // let del = document.querySelector('.del')
    document.querySelector('.tbody tr td .del').onclick = e => {
        console.log(e.target);
        
    }
    



}